package csc.fresher.view;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import csc.fresher.entity.SystemUser;
import csc.fresher.service.SystemUserService;

@Component
@Scope("session")
public class SystemUserManagementManageBean implements Serializable{
	/**
	 * 
	 */
	public SystemUserManagementManageBean() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<SystemUser> systemUserList;
	private String selectSearchType;
	private String searchValue;
	private DataModel<SystemUser> systemUserListDataModel;
	private SystemUser systemUser;
	
	

	@Autowired
	private SystemUserService systemUserService;
	
	@Autowired
	SystemLoginManageBean systemLoginManageBean;
	
//	public List<SystemUser> userStart() {
//		systemUserList = systemUserService.getSystemUserByState("active");			
//		return systemUserList;
//	}

	@PostConstruct
	public void init() {
		this.systemUser = null;
		this.systemUserList = firstLoad();
		this.systemUserListDataModel = new ListDataModel<SystemUser>(this.systemUserList);
	}
	
	//first load
	public List<SystemUser> firstLoad() {
		systemUserList = systemUserService.getSystemUser();
		return systemUserList;
	}
	//search button
		public void search() {
			if("idCardNumber".equals(selectSearchType)) {
				systemUserListDataModel = new ListDataModel<SystemUser>(systemUserService.getSystemUserByIDCardNumber(searchValue));			
			} else if("firstName".equals(selectSearchType)) {
				systemUserListDataModel = new ListDataModel<SystemUser>(systemUserService.getSystemUserByFirstName(searchValue));
			} else if("state".equals(selectSearchType)) {
				systemUserListDataModel = new ListDataModel<SystemUser>(systemUserService.getSystemUserByState(searchValue));
			} else if("phoneNumber".equals(selectSearchType)) {
				systemUserListDataModel = new ListDataModel<SystemUser>(systemUserService.getSystemUserByPhoneNumber(searchValue));
			} else if("address".equals(selectSearchType)) {
				systemUserListDataModel = new ListDataModel<SystemUser>(systemUserService.getSystemUserByAddress(searchValue));
			}
	 		//return "adas";
		}
		
		public String edit(long systemUserID) {
			systemUser = null;
			init();
			systemUser = systemUserService.get1SystemUserByID(systemUserID);
			return "ModifyUser";
		}
		
		
		public String editRole(long systemUserID) {
			boolean flag = false;
			SystemUser system = new SystemUser();
			system = systemLoginManageBean.getSystemUser();
			List<Long> listrole = systemUserService.getRoleByUserName(system.getUserName());
					
			for (int i = 0; i < listrole.size(); i++) {
				if (1 == listrole.get(i)) {
					flag = true;
					systemUser = null;
					init();
					systemUser = systemUserService.get1SystemUserByID(systemUserID);
					return "ModifyRole";
				}
			}
			if (flag == false) {
				FacesContext context = FacesContext.getCurrentInstance();
				context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "You can not use this funtion, only admin can use it!", null));	
			}
			return "";
		}
		
		public SystemUserService getSystemUserService() {
			return systemUserService;
		}

		public void setSystemUserService(SystemUserService systemUserService) {
			this.systemUserService = systemUserService;
		}

	public String getSelectSearchType() {
		return selectSearchType;
	}

	public void setSelectSearchType(String selectSearchType) {
		this.selectSearchType = selectSearchType;
	}

	public String getSearchValue() {
		return searchValue;
	}

	public void setSearchValue(String searchValue) {
		this.searchValue = searchValue;
	}
	
	public List<SystemUser> getSystemUserList() {
		return systemUserList;
	}

	public void setSystemUserList(List<SystemUser> systemUserList) {
		this.systemUserList = systemUserList;
	}

	public DataModel<SystemUser> getSystemUserListDataModel() {
		return systemUserListDataModel;
	}

	public void setSystemUserListDataModel(
			DataModel<SystemUser> systemUserListDataModel) {
		this.systemUserListDataModel = systemUserListDataModel;
	}

	public SystemUser getSystemUser() {
		return systemUser;
	}

	public void setSystemUser(SystemUser systemUser) {
		this.systemUser = systemUser;
	}
	
	
}
